#!/usr/bin/python

import os
import sys
import time
import platform
import psutil
import socket
import datetime
import urllib2
import urllib
class System:
	"""
	"""
	def __init__(self):
		"""
		"""
		self._infos = {}
	def updateInfos(self):
		"""
		"""
		self._infos["os"] = platform.system()
		self._infos["name"] = platform.node()
		self._infos["cpuUse"] = psutil.cpu_percent(interval=1)
		self._infos["cpuUsePerCore"] = psutil.cpu_percent(interval=1, percpu=True)
		cpuTime =psutil.cpu_times_percent(interval=1)
		self._infos["cpuSysRepartition"] = {"user": cpuTime[0],"system": cpuTime[1], "idle":cpuTime[2]}
		ram = psutil.virtual_memory()
		self._infos["ram"] = {"total": int(ram[0]/1024/1024),"available": int(ram[1]/1024/1024),"percent": ram[2],"used": int(ram[3]/1024/1024)}
		swap = psutil.swap_memory()
		self._infos["swap"] = {"total": int(swap[0]/1024/1024),"used": int(swap[1]/1024/1024),"percent": ram[2],"free": int(ram[3]/1024/1024)}
		disk = psutil.disk_usage('/') 
		self._infos["disk"] = {"total": int(disk[0]/1024/1024),"used": int(disk[1]/1024/1024),"free": int(disk[2]/1024/1024),"percent": disk[3]}
		self._infos["user"] = psutil.get_users()[0][0]
		self._infos["bootTime"] = datetime.datetime.fromtimestamp(psutil.get_boot_time()).strftime('%Y-%m-%d %H:%M:%S')
		s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
		s.connect(("gmail.com",80))
		self._infos["ip"] = s.getsockname()[0]
		s.close()
		
	def getInfos(self):
		"""
		"""
		return self._infos
	def sendRequest(self, url, params):
		"""
		"""
		try:
			submitUrl = url + '/run'
			postData = urllib.urlencode(params)
			fh = urllib2.urlopen(submitUrl, postData)
			jobId = fh.read()
			fh.close()
		except urllib2.HTTPError, ex:
			# Trap exception and output the document to get error message.
			print >>sys.stderr, ex.read()
			raise
		# Print job identifier
		print jobId
		
system = System()
system.updateInfos()
system.sendRequest("http://127.0.0.1:82/Bliss/trunk/Bliss/data/agent/report.php",system.getInfos())
print(system.getInfos())